package poke;

import java.util.LinkedList;
import java.util.List;
import java.util.LinkedList;
import java.util.List;

/**
 * @author ：chenzikang
 * @date ： 2020/5/12
 */

class Poke {

    String pokePoint;//牌点
    String pokeColor;//花色

    boolean inUse;//是否使用中

    public Poke() {
    }

    public boolean isInUse() {
        return inUse;
    }

    public void setInUse(boolean inUse) {
        this.inUse = inUse;
    }

    public String getPokeColor() {
        return pokeColor;
    }

    public void setPokeColor(String pokeColor) {
        this.pokeColor = pokeColor;
    }


    public String getPokePoint() {
        return pokePoint;
    }

    public void setPokePoint(String pokePoint) {
        this.pokePoint = pokePoint;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer("");
        sb.append("牌点 " + pokePoint + "\n");
        sb.append("花色 " + pokeColor + "\n");


        return sb.toString();
    }
}


public class PokeService {

    private String[] color = new String[]{"方块", "红桃", "黑桃", "梅花"};//花色数组


    private List<Poke> pokeList = new LinkedList<Poke>();//存放扑克牌

    private Poke[] poke = new Poke[52];//暂时存放所有扑克牌的对象数组，用来存放生成的54个空扑克牌对象

    public PokeService() {
    }

    //初始化扑克牌
    public void initPokeList() {
        int count = 0;//数组下标
        //初始化数组
        for (int i = 0; i < poke.length; i++) {
            poke[i] = new Poke();
        }
        //初始化list集合
        for (int i = 1; i <= 13; i++) {
            for (int j = 1; j <= 4; j++) {
                String point = Integer.toString(i);
//     当牌点，牌面大于10的时候用J，Q，K取代
                switch (i) {
                    case 11:
                        poke[count].setPokePoint("J");


                        break;
                    case 12:
                        poke[count].setPokePoint("Q");


                        break;
                    case 13:
                        poke[count].setPokePoint("K");


                        break;
                    default:
                        poke[count].setPokePoint(point);


                        break;
                }
//     设置花色
                poke[count].setPokeColor(color[j - 1]);
//     设置使用状态
                poke[count].setInUse(false);
//     放入集合中
                pokeList.add(count, poke[count]);
                count++;

            }
        }

    }

    //洗牌
    public void washPoke() {
        List<Poke> tempList = this.getPokeList();

        int times = tempList.size() - 1;

        while (times > 0) {

            int temp = (int) (Math.random() * 100) % times;

            swap(tempList, temp, times);

            times--;

        }

        this.setPokeList(tempList);
    }

    //交换LinkedList中两个已经存在的扑克牌对象
    public void swap(List<Poke> tempList, int temp, int times) {
        Poke poke1 = new Poke();
        Poke poke2 = new Poke();

        poke1 = tempList.get(temp);
        poke2 = tempList.get(times);

        tempList.remove(temp);
        tempList.add(temp, poke2);

        tempList.remove(times);
        tempList.add(times, poke1);
    }

    public List<Poke> getPokeList() {
        return pokeList;
    }

    public void setPokeList(List<Poke> pokeList) {
        this.pokeList = pokeList;
    }


    //打印
    public void print() {

        for (Poke p : pokeList) {
            System.out.println(p);
        }
    }

    // 测试方法
    public static void main(String[] args) {
        PokeService ps = new PokeService();
        ps.initPokeList();
        System.out.println(ps.getPokeList().size());
        System.out.println("*********************洗牌*****************************");
        ps.washPoke();
        ps.print();
        System.out.println("*********************洗牌完毕*****************************");
        System.out.println(ps.getPokeList().size());
    }
}